package com.wiwf.model.task.service.impl;

import java.io.IOException;

import com.wiwf.model.task.service.TaskExecService;
import com.wiwf.model.task.service.TaskHandlerService;
import com.wiwf.model.task.service.TaskOutHandlerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.wiwf.model.task.bean.TaskerCommand;

import javax.annotation.Resource;

/**
 * 任务消息输出处理服务
 *
 * @author kehui
 */
@Service
public class TaskHandlerServiceImpl implements TaskHandlerService {
	private final Logger logger = LoggerFactory.getLogger(TaskHandlerServiceImpl.class);

	@Resource
	TaskExecService taskExecService;
	@Resource
	TaskOutHandlerService taskOutHandlerService;

	@Override
	public TaskerCommand process(String id, String command) {
		TaskerCommand tasker = null;
		try {
			tasker = taskExecService.createTasker(id, command, taskOutHandlerService);
			return tasker;
		} catch (IOException e) {
			taskExecService.stopTasker(tasker);
			logger.error("{}：执行失败: {}\n\t", id, e.getMessage());
			return null;
		}
	}

	@Override
	public Boolean stop(Process process) {
		return taskExecService.stop(process);
	}

	@Override
	public Boolean stop(Thread thread) {
		return taskExecService.stop(thread);
	}

	@Override
	public Boolean stop(Process process, Thread thread) {
		boolean ret = false;
		ret = stop(thread);
		ret = stop(process);
		return ret;
	}

}
